<?php
if (isset($_SESSION['admin-login'])) {
	if (isset($_SESSION['status-featured'])) {
		echo "<p>".$_SESSION['status-featured']."</p>";
		unset($_SESSION['status-featured']);
	}
	$featured = array();
	$temp = db_array("SELECT * FROM produk WHERE featured = 1 LIMIT 0,3");
	if ($temp != false) {
		foreach ($temp as $record) {
			$featured[] = $record['id'];
		}
	}
	$produks = db_array("SELECT * FROM produk ORDER BY nama ASC");
	?>
	<form action="<?php echo site()."/".$param[0] ?>/featured-action" method="post" onsubmit="return validateFeatured()">
		<select name="featured1">
			<?php
			foreach ($produks as $produk) {
				echo "<option value=\"".$produk['id']."\"";
				if ($produk['id'] == $featured[0]) echo " selected=\"selected\"";
				echo ">".$produk['nama']."</option>";
			}
			?>
		</select>
		<select name="featured2">
			<?php
			foreach ($produks as $produk) {
				echo "<option value=\"".$produk['id']."\"";
				if ($produk['id'] == $featured[1]) echo " selected=\"selected\"";
				echo ">".$produk['nama']."</option>";
			}
			?>
		</select>
		<select name="featured3">
			<?php
			foreach ($produks as $produk) {
				echo "<option value=\"".$produk['id']."\"";
				if ($produk['id'] == $featured[2]) echo " selected=\"selected\"";
				echo ">".$produk['nama']."</option>";
			}
			?>
		</select>
		<input type="submit" name="update" id="update" value="Update" disabled="disabled" />
	</form>
	<script type="text/javascript">
		document.getElementById("update").removeAttribute("disabled");
		function validateFeatured() {
			var body = document.getElementsByTagName("body")[0];
			var form = document.getElementsByTagName("form")[0];
			var featured1 = form["featured1"].value;
			var featured2 = form["featured2"].value;
			var featured3 = form["featured3"].value;
			var status = document.getElementsByTagName("p")[0] || document.createElement("p");
			if (featured1 == featured2 || featured1 == featured3 || featured2 == featured3) {
				status.innerHTML = "Produk featured tidak boleh sama";
				body.insertBefore(status,form);
			} else {
				if (document.getElementsByTagName('p')[0])
					body.removeChild(document.getElementsByTagName('p')[0]);
				return true;
			}
			return false;
		}
	</script>
	<?php
} else {
	$_SESSION['status-login'] = "Log in terlebih dahulu untuk melanjutkan";
	header("Location: ".site()."/".$param[0]."/login");
}
?>